package com.team24.hotel.service.impl;

import com.team24.hotel.domain.*;
import com.team24.hotel.mapper.GoodMapper;
import com.team24.hotel.mapper.GoodOrderMapper;
import com.team24.hotel.mapper.SupplierMapper;
import com.team24.hotel.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private GoodOrderMapper goodOrderMapper;
    public List<Supplier> listSupplier(){ return supplierMapper.selectByExample(new SupplierExample());}

    @Override
    public List<Good> listGood() {
        return goodMapper.selectGoodExt();
    }
    @Override
    public int insertGood(Good good) {
        return goodMapper.insert(good);
    }

    @Override
    public void deleteGoodByPrimaryKey(Integer id) {

        List<GoodOrder> goodOrders = goodOrderMapper.selectByGoodId(id);
        for (GoodOrder goodOrder : goodOrders) {
            goodOrderMapper.deleteByPrimaryKey(goodOrder.getGoodOrderId());
        }
        goodMapper.deleteByPrimaryKey(id);

    }


    @Override
    public Good findGoodById(Integer id) {
        return goodMapper.selectByPrimaryKey(id);
    }

    @Override
    public int saveOrUpdate(Good good) {
        if(good.getGoodId()==null){
            return goodMapper.insert(good);
        }else {
            return goodMapper.updateByPrimaryKeySelective(good);
        }
    }

}

